package yxy.game.pm2.active.command.flop;

import yxy.apple.logger.Logger;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.task;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_翻牌;
import yxy.game.pm2.active.module.subordinateclass.flop.FlopActive;
import yxy.game.pm2.active.module.subordinateclass.flop.FlopActiveTask;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;

/**
 * 翻牌 - 活跃任务
 * @author Dell
 *
 */
@MessageType(proto = Command.ActFlopCardTaskDrawReward, classtype = ActiveMessage.ActFlopCardTaskDrawRewardRequest.class)
public class CMD_Active_翻牌_活跃任务_TaskDrawReward extends Command<ActiveMessage.ActFlopCardTaskDrawRewardRequest> {

	private static final Logger logger = Logger.create(CMD_Active_翻牌_活跃任务_TaskDrawReward.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActFlopCardTaskDrawRewardRequest request) throws Exception {
		String playercode = messageSource.getTag();
		int id = request.getId();
		ActiveMessage.ActFlopCardTaskDrawRewardResponse.Builder response = ActiveMessage.ActFlopCardTaskDrawRewardResponse.newBuilder();
		
		ActiveModule_翻牌 am = ActiveModule_翻牌.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		FlopActive fa = am.getFa();
		
		//活跃任务
		for(FlopActiveTask fat : fa.getFats()) {
			if(fat.getId() == id) {
				String taskcode = fat.getTaskcode();
				Task taskbean = Task.get(playercode, taskcode);
				
				if(taskbean.isReward()) {
					send(messageSource, response, Result.ActHadGet);
					return;
				}
				
				if(!taskbean.isFinish()) {
					send(messageSource, response, Result.ActCannotGet);
					return;
				}
				
				task taskdata = DataCenter.getData(fat.getTaskid(), task.class);
				if(taskdata == null) {
					send(messageSource, response, Result.DataError);
					return;
				}
				
				Collection<Currency> curs = Currency.create(taskdata.getRewardtype(), taskdata.getRewardid(), taskdata.getRewardnum());
				Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
				
				taskbean.setReward(true);
				taskbean.set();
				
				int newfinishnum = fa.getFinishactivetasknum() + 1;
				fa.setFinishactivetasknum(newfinishnum);
				am.set();
				
				response.addAllCurs(items);
				new CurrencyHandle().addCurrency(playercode, CCReason.活动_翻牌, curs);
				send(messageSource, response, Result.Success);
				return;
			}
		}
		
		send(messageSource, response, Result.DataError);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActFlopCardTaskDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
